//
//  VisitorLoginView.swift
//  HyWeibo
//
//  Created by HY-Mac on 16/6/17.
//  Copyright © 2016年 HY-Mac. All rights reserved.
//

import UIKit

@objc protocol VisitorLoginViewDelegate {
  optional func visitorWillLogin()
    func visitorWillRegister()
}

class VisitorLoginView: UIView {
    
    var visitorDelegate: VisitorLoginViewDelegate?
    
    func loginBtnDidClick(){
        visitorDelegate?.visitorWillLogin?()
        
    }
    func registerBtnDidClick(){
        visitorDelegate?.visitorWillRegister()
    }
    
    init() {
        super.init(frame: CGRectZero) // 设置为zero没有影响，在赋值给view的时候会再次赋值
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("  ")
    }
    
    func setupVistorInfo(image: String?, title: String){
        iconView.hidden = false
        if image != nil {
            iconView.hidden = true
            circleView.image = UIImage(named: image!)
        }else{
            startAnimation()
        }
        tipLabel.text = title
    }
    func startAnimation() {
        let anim: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    private func setupUI(){
       // self.backgroundColor = UIColor.clearColor()
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置布局
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -50))
        
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 50))
        //设置宽度
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 300))
        //设置登录
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .CenterY, relatedBy: .Equal, toItem: tipLabel, attribute: .CenterY, multiplier: 1.0, constant: 50))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        //注册
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: -30))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .CenterY, relatedBy: .Equal, toItem: tipLabel, attribute: .CenterY, multiplier: 1.0, constant: 50))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView ,"registerBtn": registerBtn]))
        
        /////////////
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
    }
    
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.text = "关注一些人，回这里看看有什么惊喜"
        l.textAlignment = NSTextAlignment.Center
        l.font = UIFont.systemFontOfSize(14.0)
        l.textColor = UIColor.lightGrayColor()
        l.numberOfLines = 0
        l.sizeToFit()
        return l
    }()
    private lazy var loginBtn: UIButton = {
        var btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.addTarget(self, action: #selector(VisitorLoginView.loginBtnDidClick), forControlEvents: .TouchUpInside)
        
        return btn
    }()
    private lazy var registerBtn: UIButton = {
        var btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.addTarget(self, action: #selector(VisitorLoginView.registerBtnDidClick), forControlEvents: .TouchUpInside)
        return btn
    }()
    

}
